﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DAO;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;
using ESoft.Core.Interceptor;

namespace Edu.Web.Data.Service.Impl
{
    public class StudentService : IStudentService
    {

        private readonly IUserService _userService;
        private readonly IClazzStuDAO _clazzStuDao;
        private readonly IStudentDAO _studentDao;

        public StudentService(IUserService userService, IClazzStuDAO clazzStuDao, IStudentDAO studentDao)
        {
            _userService = userService;
            _clazzStuDao = clazzStuDao;
            _studentDao = studentDao;
        }


        [TransactionalAttribute]
        public void SaveStudentOfClazz(List<Dictionary<String, object>> userStudentList, long clazzId)
        {
            foreach (var userStudent in userStudentList)
            {
                UserEntity user = new UserEntity();

                // 学号作为用户名
                user.UserName = userStudent["StuNo"].ToString();
                user.Gender = Convert.ToInt32(userStudent["Gender"]);
                user.RealName = userStudent["RealName"].ToString();
                user.UserPassword = userStudent["StuNo"].ToString();
                user.UserType = 1;
                long userId = _userService.RegisterUser(user);

                StudentEntity stu = new StudentEntity();
                stu.UserId = userId;
                stu.StuNo = userStudent["StuNo"].ToString();
                stu = _studentDao.Add(stu);
                ClazzStuEntity clazzStu = new ClazzStuEntity();
                clazzStu.ClazzId = clazzId;
                clazzStu.StuUserId = stu.UserId;
                _clazzStuDao.Add(clazzStu);
            }
        }

        public StudentInfoDV LoadStudentInfo(long studentId)
        {
            return _studentDao.FindInfoByStudentId(studentId);
        }

        public List<ClassStudentDV> LoadStudentListByClazzId(long clazzId)
        {
            return _studentDao.LoadStudentListByClazzId(clazzId).ToList();
        }
    }
}